package com.daedalus.File.guessWord;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: kostika
 * Date: 12/27/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class GuessWord {
    public static void main(String[] args){

        File skedari;
        Scanner fileReader;
        Scanner keyboardReader = new Scanner(System.in);
        ArrayList<String> fjalet = new ArrayList<String>();
        Random r = new Random();

        skedari=new File("fjalet.txt");

        try {
            fileReader = new Scanner(skedari);

            while (fileReader.hasNext()){
                fjalet.add(fileReader.next());
            }

        } catch (FileNotFoundException e) {
            System.err.println("Error opening File");
            return;
        }

        //zgjidh nje fjale cfaredo
        String fjalaRandom = fjalet.get(r.nextInt(fjalet.size()));

        // per fjalen e zgjedhur ndertoj nje strukture qe mban karakteret
        // dhe statusin e karakterit (nese eshte zbuluar apo jo)
        char[] karakteret = fjalaRandom.toCharArray();
        boolean[] found = new boolean[fjalaRandom.length()];

        int noFoundCharacters = 0;
        int noErrors = 0;
        do{
            System.out.println("Fjala per tu gjetur :");
            for (int i=0; i<karakteret.length; i++){
                if (found[i]){    //nese shkronja eshte gjetur
                    System.out.print(" "+karakteret[i]);
                } else {           //nese nuk eshte zbuluar ende
                    System.out.print(" _");
                }
            }
            System.out.println("\nSupozoni nje shkronje :");
  //marr karakterin e pare qe me jepet nga tastjera
            char supposedChar=keyboardReader.next().charAt(0);
            boolean successOne = false;  //skam gjetur asnje shkronje te njejte
   //kontrolloj ne vektorin e karaktereve
            for (int i=0; i<karakteret.length; i++){
                if (karakteret[i]==supposedChar && !found[i]){ //duhet te mos jete gjetur me pare
                    found[i]=true;
                    noFoundCharacters++;
                    successOne = true;
                }
            }
            if (!successOne){   //nese nuk gjeta asnje karakter te njejte
                noErrors++;
            }
            if (noErrors>=5){         //nese plotesoneh 5 gabime
                System.err.println("Ju keni kryer 5 Gabime !! Provoni perseri me vone");
                return;
            }
        }while (noFoundCharacters<fjalaRandom.length());
        System.out.println("Urime !!! E keni gjetur Fjalen!");

    }


}
